const express = require('express')
const app = express()
const path = require('path')
//启动中间健
app.use(express.json())
app.use(express.urlencoded({extended:false}))

app.post('/postData',(req,res)=>{

  const {
    name,
    sex,
    age
  }=req.body
  // console.log(req.body)
  if (!name||!sex||!age){
    return res.send({
      code:10001,
      msg:'提交数据失败',
      data:null
    })
  }

  res.send({
    code:10000,
    msg:'提交数据成功',
    data:{
      name,
      sex,
      age
    }
  })
})

app.get('/user',(req,res)=>{

  const {
    userId
  } = req.query

  if (userId !== '001'){
    return res.send({
      code:10001,
      message:'查询数据失败',
      data:null
    })
  }

  res.send({
    code:10000,
    message:'查询数据成功',
    data:{
      userId:'001',
      userName:'万登豪',
      userSex:'男',
      userAge:18
    }
  })

})

app.get('/userGet',(req,res)=>{

  const {
    userId
  } = req.query

  if (userId !== '001'){
    return res.send({
      code:10001,
      message:'查询数据失败',
      data:null
    })
  }

  res.send({
    code:10000,
    message:'查询数据成功',
    data:{
      userId:'001',
      userName:'万登豪',
      userSex:'男',
      userAge:18
    }
  })

})


app.get('/1',(req,res)=>{

  const filePath = path.resolve(__dirname,'./jQueryAjax.html')
  res.sendFile(filePath)

})

app.listen(8090,()=>{
  console.log('running!!! 可以访问 http://127.0.0.1:8090')
})